<script type="text/javascript">
    $(function() {
        $('#admin_Weikeypoint_datagrid').datagrid({
            url: APP + '/Weikeypoint/view',
            fit: true,
            // fitColumns: true,
            border: false,
            pagination: true,
            idField: 'id',
            treeField: 'title',
            parentField: 'pid',
            pageSize: 50,
            pageList: [10, 20, 30, 40, 50, 80, 100],
            sortName: 'title',
            sortOrder: 'asc',
            checkOnSelect: true,
            selectOnCheck: false,
            nowrap: true,
            singleSelect: true,
            frozenColumns: [
                [{
                    title: '编号',
                    field: 'id',
                    width: 50,
                    sortable: true,
                    checkbox: false
                }, {
                    title: '添加时间',
                    field: 'addtime',
                    width: 150,
                    hidden: false
                }]
            ],
            columns: [
                [{
                    title: 'openid',
                    field: 'weiuser_openid',
                    width: 200,
                    hidden: false
                }, {
                    title: '名称',
                    field: 'weiuser',
                    width: 100,
                    sortable: true,
                    formatter: function(value, row, index) {
                        // console.info(value);
                        if (value != null) {
                            return value.nickname;
                        } else {
                            return '<font color=red>空</font>';
                        }
                    }
                }, {
                    title: '网络',
                    field: 'network',
                    width: 60,
                    hidden: false
                }, {
                    title: '经度',
                    field: 'lng',
                    width: 100,
                    hidden: false
                }, {
                    title: '纬度',
                    field: 'lat',
                    width: 100,
                    hidden: false
                }, {
                    title: '关键点名称',
                    field: 'title',
                    width: 100,
                    hidden: false
                }, {
                    title: '备注',
                    field: 'remark',
                    width: 100,
                    hidden: false
                }, {
                    title: '地址',
                    field: 'address',
                    width: 200,
                    hidden: false
                }, {
                    title: '照片',
                    field: 'img',
                    width: 150,
                    hidden: false,
                    formatter: function(value, row, index) {
                        //console.info(value);
                        if (!value == '') {
                            return '<img src=' + ROOT + "/Wll/Tpl/Res/upload/" + value + ' width="100" height="100" border="0" />';
                        } else {
                            return '<font color=red>空</font>';
                        }
                    }
                }]
            ],
            toolbar: [{
                    text: '刷新',
                    iconCls: 'icon-reload',
                    handler: function() {
                        $('#admin_Weikeypoint_datagrid').datagrid('reload');
                    }
                }, <?php if(in_array("Weikeypoint/add",$_SESSION['user']['nodesname'])){ ?>
                '-', {
                    text: '增加',
                    iconCls: 'icon-add',
                    handler: function() {
                        admin_Weikeypoint_appendFun();
                    }
                }, <?php }?>
                <?php if(in_array("Weikeypoint/delete",$_SESSION['user']['nodesname'])){ ?>
                '-', {
                    text: '批量删除',
                    iconCls: 'icon-remove',
                    handler: function() {
                        admin_Weikeypoint_removeFun();
                    }
                }, <?php }?>
                <?php if(in_array("Weikeypoint/edit",$_SESSION['user']['nodesname'])){ ?>
                '-', {
                    text: '编辑',
                    iconCls: 'icon-edit',
                    handler: function() {
                        admin_Weikeypoint_editFun();
                    }
                }, <?php }?>
                '-'
            ],
            onDblClickRow: function(rowIndex, rowData) {
                // console.info(rowData);
                $("#layout_center_tabs").tabs("select", "首页");
                document.getElementById('mhgis').contentWindow.addKeypointOnlyGraphics(rowData, 'keypoint');
            }
        });
    });

    function admin_Weikeypoint_editFun() {
        var rows = $('#admin_Weikeypoint_datagrid').datagrid('getChecked');
        //console.info(rows);
        if (rows.length > 1) {
            showMsg('不能同时编辑多条记录，请选择一条！');
            $('#admin_Weikeypoint_datagrid').datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
        } else if (rows.length == 0) {
            showMsg('请勾选要编辑的记录！');
        } else {
            $('<div/>').dialog({
                href: TMPL + '/Weikeypoint/edit.html',
                width: 700,
                top: 0,
                height: 550,
                modal: true,
                title: '编辑',
                buttons: [{
                    text: '编辑',
                    iconCls: 'icon-edit',
                    handler: function() {
                        var d = $(this).closest('.window-body');
                        $.messager.progress({
                            title: '请稍候',
                            msg: '正在提交！请稍后...'
                        });
                        $('#admin_WeikeypointEdit_Form').form('submit', {
                            url: APP + '/Weikeypoint/edit',
                            onSubmit: function() {
                                var isValid = $(this).form('validate');
                                if (!isValid) {
                                    $.messager.progress('close'); // 如果表单是无效的则隐藏进度条
                                }
                                return isValid; // 返回false终止表单提交
                            },
                            success: function(r) {
                                $.messager.progress('close');
                                if (r.status == 1) {
                                    $('#admin_Weikeypoint_datagrid').datagrid('reload');
                                    d.dialog('destroy');
                                    showMsg(r.info);
                                } else {
                                    showMsg(r.info);
                                }
                            }
                        });
                    }
                }],
                onClose: function() {
                    $(this).dialog('destroy');
                },
                onLoad: function() {
                    //var index = $('#admin_Weikeypoint_datagrid').datagrid('getRowIndex', id);
                    var rows = $('#admin_Weikeypoint_datagrid').datagrid('getChecked');
                    var o = rows[0];
                    //                      var resourceIds = new Array();
                    //                      $.each(o.nodes, function(i, n){
                    //                          //console.info(value[i].title);
                    //                          //resourceIds[i]= parseInt(o.nodes[i].id);
                    //                          resourceIds[i] = o.nodes[i].id;
                    //                      //li[i].replace(/\"/g,"");
                    //                      //stringToList((o.nodes)[0].id)
                    //                      });
                    //                      o.resourceIds = resourceIds;
                    //console.info(o);
                    //o.resourceIds = stringToList(rows[index].resourceIds);
                    $('#admin_WeikeypointEdit_Form').form('load', o);
                    var imgurl = $("#icon").val();
                    $("#imgShow").attr('src', ROOT + '/Weixin/Tpl/Res/upload/Weikeypoint/' + imgurl);
                }
            });
        }
    }

    function admin_Weikeypoint_appendFun() {
        $('#admin_Weikeypoint_datagrid').datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
        $('<div/>').dialog({
            href: TMPL + '/Weikeypoint/add.html',
            width: 700,
            top: 0,
            height: 550,
            modal: true,
            title: '添加',
            buttons: [{
                text: '增加',
                iconCls: 'icon-add',
                handler: function() {
                    var d = $(this).closest('.window-body');
                    $.messager.progress({
                        title: '请稍候',
                        msg: '正在提交！请稍后...'
                    });
                    $('#admin_WeikeypointAdd_addForm').form('submit', {
                        url: APP + '/Weikeypoint/add',
                        onSubmit: function() {
                            var isValid = $(this).form('validate');
                            if (!isValid) {
                                $.messager.progress('close'); // 如果表单是无效的则隐藏进度条
                            }
                            return isValid; // 返回false终止表单提交
                        },
                        success: function(r) {
                            $.messager.progress('close');
                            if (r.status == 1) {
                                $('#admin_Weikeypoint_datagrid').datagrid('reload');
                                showMsg(r.info);
                            } else {
                                showMsg(r.info);
                            }
                        }
                    });
                }
            }],
            onClose: function() {
                $(this).dialog('destroy');
            }
        });
    }

    function admin_Weikeypoint_removeFun() {
        var rows = $('#admin_Weikeypoint_datagrid').datagrid('getChecked');
        var ids = [];
        if (rows.length > 0) {
            $.messager.confirm('确认', '您是否要删除当前选中的病种？删除后此病种关联的问题将不可见，请慎重！！！！！！！！！！！！', function(r) {
                if (r) {
                    for (var i = 0; i < rows.length; i++) {
                        ids.push(rows[i].id);
                    }
                    $.ajax({
                        url: APP + '/Weikeypoint/delete',
                        data: {
                            ids: ids.join(',')
                        },
                        dataType: 'json',
                        success: function(r) {
                            if (r.status) {
                                $('#admin_Weikeypoint_datagrid').datagrid('load');
                                $('#admin_Weikeypoint_datagrid').datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
                            }
                            showMsg(r.info);
                        }
                    });
                }
            });
        } else {
            showMsg('请勾选要删除的记录！');
        }
    }

    function admin_Weikeypoint_deleteFun(id) {
        $('#admin_Weikeypoint_datagrid').datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
        $('#admin_Weikeypoint_datagrid').datagrid('checkRow', $('#admin_Weikeypoint_datagrid').datagrid('getRowIndex', id));
        admin_Weikeypoint_removeFun();
    }
</script>
<table id="admin_Weikeypoint_datagrid">
</table>